package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.ShopEntity;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.user.LoginActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomeFragmentRecommendAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomepageBannerViewPager;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;
import com.wanglibao.woodtrade.woodtrade.util.CallPhone;
import com.wanglibao.woodtrade.woodtrade.widget.SVListView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopActivity extends BaseActivity implements AdapterView.OnItemClickListener{

    @Bind(R.id.rl_common_title)
    RelativeLayout rlCommonTitle;

    @Bind(R.id.tv_activity_shop_phone)
    TextView tvActivityShopPhone;

    @Bind(R.id.vp_activity_shop_imglist)
    ViewPager vpActivityShopImglist;

    @Bind(R.id.sdv_activity_shop_icon)
    ImageView sdv_activity_shop_icon;

    @Bind(R.id.tv_activity_shop_name)
    TextView tvActivityShopName;

    @Bind(R.id.tv_activity_shop_com_info)
    TextView tvActivityShopComInfo;

    @Bind(R.id.sv_activity_shop)
    ScrollView sv_activity_shop;

    @Bind(R.id.list_activity_shop_commoditylist)
    SVListView listActivityShopCommoditylist;

    @Bind(R.id.ll_activity_shop_dot)
    LinearLayout ll_activity_shop_dot;

    @Bind(R.id.tv_activity_shop_store_hot)
    TextView tv_activity_shop_store_hot;

    ShopEntity shopEntity;

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2) {
                if (msg.arg1 == NetConfig.TaskId.storeDetail){
                    shopEntity = new Gson().fromJson(msg.obj.toString(), ShopEntity.class);
                    showView();
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, 0, 0, 0);
        }

        addViewClicklistener(tvBack);
        tvTitle.setText("店铺详情");
        String id = getIntent().getStringExtra("id");
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER+NetConfig.TYPE_STORE,
                NetConfig.storeDetail,handler,"jsonObject",NetConfig.TaskId.storeDetail,new Object[]{id});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @OnClick(R.id.tv_activity_shop_phone) void phoneClick(){
        if (new UserImpl(this).isLogin()){
            CallPhone.call(shopEntity.getMobile(),this);
        }else{
            startActivity(new Intent(this, LoginActivity.class));
        }

    }

    public void showView(){
        tvActivityShopName.setText(shopEntity.getCompany());
        ImageOptions imageOptions = new ImageOptions.Builder().setLoadingDrawable(getResources().getDrawable(R.mipmap.ic_lose_p_loading))
                .setFailureDrawable(getResources().getDrawable(R.mipmap.ic_lose_p_fail)).
                        setCircular(true).build();
        x.image().bind(sdv_activity_shop_icon,shopEntity.getLogo_img_url(),imageOptions);
        tvActivityShopComInfo.setText(shopEntity.getCom_info());
        tv_activity_shop_store_hot.setText("热度: "+shopEntity.getStore_hot());

        HomepageBannerViewPager homepageBannerViewPager = new HomepageBannerViewPager(this,vpActivityShopImglist,shopEntity.getImg_url(),ll_activity_shop_dot);
        homepageBannerViewPager.initScrollView();

        Commodity commodity = new Commodity();
        commodity.setList(shopEntity.getGoodslist());
        HomeFragmentRecommendAdapter homeFragmentRecommendAdapter = new HomeFragmentRecommendAdapter(this,commodity);
        homeFragmentRecommendAdapter.setIsShowHotNo();
        listActivityShopCommoditylist.setAdapter(homeFragmentRecommendAdapter);
        listActivityShopCommoditylist.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sv_activity_shop.smoothScrollTo(0,0);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,CommodityActivity.class);
        intent.putExtra("id",shopEntity.getGoodslist().get(position).getGoods_id());
        startActivity(intent);
    }
}
